package com.frank.markdown_editor.tools;

import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

import com.frank.markdown_editor.activity.ViewPagerActivity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class IOTools {
    public final static String PATH =
            Environment.getExternalStorageDirectory()+ "/MarkDown编辑器/";
    File f = new File(PATH);

    private static File file;

    public void dir(){
        file = new File(PATH);
        if (!file.exists()){
            file.mkdirs();
        }
    }

    public void deleteFile(String fileName){
        File file = new File(PATH+"/"+fileName);
        if (file.exists()){
            file.delete();
            System.out.println("已删除！");
        }else {
            System.out.println("没有该文件。。。"+fileName);
        }
    }

    public File[] getFiles(Context context){
        File file = new File(PATH);
        if (file.list() == null){
            Toast.makeText(context,"没有...",Toast.LENGTH_SHORT).show();
            return null;
        }
        return file.listFiles();
    }

    public static String getFormatTime(Long time){
        Date date = new Date(time);
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        return df.format(date);
    }

    public static String getSize(File file){
        long fileSize = file.length();
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        if (fileSize == 0){
            fileSizeString = "00B";
        }else if (fileSize < 1024) {
            fileSizeString = df.format((double) fileSize) + "B";
        } else if (fileSize < 1048576) {
            fileSizeString = df.format((double) fileSize / 1024) + "K";
        } else if (fileSize < 1073741824) {
            fileSizeString = df.format((double) fileSize / 1048576) + "M";
        } else {
            fileSizeString = df.format((double) fileSize / 1073741824) +"G";
        }
        return fileSizeString;
    }

    public boolean addMDFile(String content, Context context){
        if (content == "" || content.matches("^[\\s ]*[\\r]*[\\n]*")){
            Toast.makeText(context,"请填入文件名！",Toast.LENGTH_SHORT).show();
            return false;
        }
        File file_1 = new File(PATH);
        for (int i = 0;i < file_1.listFiles().length;i ++){
            if (file_1.listFiles()[i].getName().equals(content+".md")){
                System.out.println("已存在。。。");
                return false;
            }
        }
        File file = new File(PATH+"/"+content+".md");
        try {

            if (!file.exists()) {
                file.createNewFile();
            }
            Toast.makeText(context, "成功！", Toast.LENGTH_SHORT).show();
            return true;

        }catch (IOException e){
            e.printStackTrace();
            return false;
        }
    }

    public StringBuilder fileRead(String fileName){

        StringBuilder builder = new StringBuilder();

        try {
            BufferedReader BReader = new BufferedReader(new FileReader(f+"/"+fileName));
            String s ;
            while((s = BReader.readLine()) != null){
                builder.append(s+"\n");
            }

        } catch (FileNotFoundException e) {
            System.out.println("没有找到该文件！");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder;
    }

    public static void saveAsHTML(String fileName,String fileContent,Context context){
        File f = new File(PATH+"/");
        BufferedWriter BWriter = null;
        try {
            BWriter = new BufferedWriter(new FileWriter(f+"/"+fileName+".html"));
            BWriter.write(fileContent);
            BWriter.flush();
            Toast.makeText(context,"成功！",Toast.LENGTH_SHORT).show();
        }catch (IOException e){
            Toast.makeText(context,"失败！",Toast.LENGTH_SHORT).show();
        }finally {
            try {
                if (BWriter != null){
                    BWriter.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    public void saveFile(String fileName,String fileContent){
        File f = new File(PATH+"/");
        try {

            BufferedWriter BWriter = new BufferedWriter(new FileWriter(f+"/"+fileName));
            BWriter.write(fileContent);
            BWriter.flush();

        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void renameFile(String file, String toFile) {
        File toBeRenamed = new File(file);
        //检查要重命名的文件是否存在，是否是文件
        if (!toBeRenamed.exists() || toBeRenamed.isDirectory()) {
            System.out.println("File does not exist: " + file);
            return;
        }

        File newFile = new File(toFile);
        //修改文件名
        if (toBeRenamed.renameTo(newFile)) {
            System.out.println("File has been renamed.");
        } else {
            System.out.println("Error renmaing file");
        }


    }


//    File file = new File(Environment.getExternalStorageDirectory(),"_Test_测试_.txt");

//    public void initDir(){
//        for (int i = 0;i > 21;i ++){
//            f= new File(PATH+"/Test_"+i);
//            if (!f.exists()){
//                f.mkdirs();
//            }
//        }
//    }

//    public boolean isExternalStorageState(){
//        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//            //存在SD卡
//            return true;
//        }else {
//            //不存在SD卡
//            return false;
//        }
//    }
//
//    public void creatFile(String fileName){
//        if (!isExternalStorageState()){
//            return;
//        }
//        File file = new File(Environment.getExternalStorageDirectory(),fileName+".md");
//        System.out.println(isExternalStorageState());
//        try {
//            FileOutputStream output = new FileOutputStream(file,true);
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//    }
//
//    public void Files(){
//        if (!isExternalStorageState()){
//            return;
//        }
//
//    }

}
